package com.cndym.utils;

import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;

public class JdbcPageUtil {

    /**
     * 数据分页查询
     *
     * @param queryString:SQL
     * @param page,起始索引
     * @param pageSize,分页大小
     * @return
     */
    private String getPageSQL(String queryString, Integer page, Integer pageSize) {
        String pageSQL = this.getOraclePageSQL(queryString, page, pageSize);
        return pageSQL;
    }

    /**
     * 构造 Oracle数据分页SQL
     *
     * @param queryString
     * @param page
     * @param pageSize
     * @return
     */
    public static String getOraclePageSQL(String queryString, Integer page, Integer pageSize) {
        if (StringUtils.isEmpty(queryString)) {
            return null;
        }
        int startIndex = (page - 1) * pageSize;
        int endIndex = page * pageSize;
        String endSql = "select * from (select rOraclePageSQL.*,ROWNUM as currentRow from (" +
                queryString + ") rOraclePageSQL where rownum <=" + endIndex + ") where currentRow>" + startIndex;
        return endSql;
    }

    private String getCountQuerySQL(String queryString) {
        String sql = "";
        if (StringUtils.isNotEmpty(queryString)) {
            sql = "select count(*) from (" + queryString + ") xCount";
        }
        return sql;
    }


    public Integer getCount(JdbcTemplate jdbcTemplate, String queryString, Object [] obj) {
        Integer count = jdbcTemplate.queryForInt(queryString, obj);
        return count;
    }
}
